package com.zhongfl.crm.lixiao.batch.service.lx2crm;

import com.heptagram.lixiao.api.LiXiaoService;
import com.heptagram.lixiao.bean.response.LXDepartmentResponse;
import com.heptagram.lixiao.common.exception.LiXiaoErrorException;
import com.zhongfl.crm.enums.UserMatchStatus;
import com.zhongfl.crm.lixiao.bean.resolve.DepartmentModelResolver;
import com.zhongfl.crm.lixiao.config.MailConfigInfo;
import com.zhongfl.crm.mail.MailService;
import com.zhongfl.crm.mail.bean.MailMessage;
import com.zhongfl.crm.mail.bean.MailMessageBuilder;
import com.zhongfl.crm.mail.enums.MailTypeEnum;
import com.zhongfl.crm.model.LxDepartment;
import com.zhongfl.crm.model.boss.Amiba;
import com.zhongfl.crm.service.LxDepartmentService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 励销部门信息同步
 *
 * @author wang.yuchuan
 * @date 2018/6/24
 */
@Service
@Slf4j
public class LxDepartmentSyncService {

    @Resource
    private LiXiaoService liXiaoService;

    @Resource
    private LxDepartmentService lxDepartmentService;

    @Resource
    private MailService mailService;

    @Resource
    private MailConfigInfo mailConfigInfo;

    public void sync() throws LiXiaoErrorException {
        LXDepartmentResponse lxDepartmentResponse = liXiaoService.getUserService().findAllDepartments();

        // 励销所有部门
        List<LXDepartmentResponse.Department> lxDepartments = lxDepartmentResponse.getData().getOptions();
        if (CollectionUtils.isEmpty(lxDepartments)) {
            return;
        }

        List<LxDepartment> notMatchDepartments = new ArrayList<>();

        for (LXDepartmentResponse.Department lxDepartment : lxDepartments) {
            LxDepartment localLxDepartment = lxDepartmentService.findByLxId(lxDepartment.getId());

            if (Objects.isNull(localLxDepartment)) {
                log.info("励销部门ID:{} name:{} 本地不存在, 新建之", lxDepartment.getId(), lxDepartment.getName());
                DepartmentModelResolver departmentModelResolver = new DepartmentModelResolver();
                LxDepartment resolvedDepartment = departmentModelResolver.resolve(lxDepartment);

                // 匹配阿米巴
                matchAmiba(resolvedDepartment);

                if (resolvedDepartment.getMatchStatus().equals(UserMatchStatus.NO)) {
                    // 如果没有匹配到amiba则不插入
                    log.info("部门:{}没有匹配到阿米巴信息", lxDepartment.getName());
                    notMatchDepartments.add(resolvedDepartment);
                    continue;
                }

                lxDepartmentService.insertDepartment(resolvedDepartment);
            } else {

                LxDepartment updateLxDepartment = new LxDepartment();
                updateLxDepartment.setId(localLxDepartment.getId());
                updateLxDepartment.setName(lxDepartment.getName());
                updateLxDepartment.setLxId(lxDepartment.getId());
                updateLxDepartment.setParentId(lxDepartment.getParentId());
                updateLxDepartment.setLevel(lxDepartment.getLevel());
                lxDepartmentService.updateDepartmentById(updateLxDepartment);
                log.info("励销部门id:{} name:{} 本地已存在, 跳过处理", lxDepartment.getId(), lxDepartment.getName());
            }
        }

        sendNoticeMail(notMatchDepartments);
    }

    private void matchAmiba(LxDepartment lxDepartment) {
        Amiba amiba = lxDepartmentService.findAmibaByName(lxDepartment.getName());

        if (Objects.isNull(amiba)) {
            lxDepartment.setMatchStatus(UserMatchStatus.NO);
            lxDepartment.setLxStatus(UserMatchStatus.NO);
        } else {
            lxDepartment.setMatchStatus(UserMatchStatus.YES);
            lxDepartment.setLxStatus(UserMatchStatus.YES);
            lxDepartment.setAmibaId(amiba.getId());
            lxDepartment.setAmibaName(amiba.getName());
        }
    }

    private void sendNoticeMail(List<LxDepartment> lxDepartments) {
        if (CollectionUtils.isEmpty(lxDepartments)) {
            return;
        }
        log.info("没有匹配到本地阿米巴的数量为{}", lxDepartments.size());
        MailMessage mailMessage = MailMessageBuilder.builder()
            .mailTypeEnum(MailTypeEnum.LX_DEPARTMENT_SYNC_ERROR)
            .addData("title", "BOSS  没有匹配到本地阿米巴部门列表:")
            .addData("data", lxDepartments)
            .subject("励销部门同步异常通知-" + mailConfigInfo.getActiveProfiles())
            .to(mailConfigInfo.getDefaultTo())
            .build();
        mailService.send(mailMessage);
    }
}
